package com.lagou.service.user.controller;

import com.lagou.service.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册接⼝，true成功，false失败
     */
    @PostMapping("/register/{email}/{password}/{code}")
    public Integer register(@PathVariable @NotBlank(message = "邮箱不能为空") String email,
                            @PathVariable @NotBlank(message = "密码不能为空") String password,
                            @PathVariable @NotBlank(message = "验证码不能为空") String code) {
        return userService.register(email, password, code);
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable @NotBlank(message = "邮箱不能为空") String email) {
        return userService.isRegistered(email);
    }

    /**
     * 登录接⼝，验证用户名密码合法性，根据用户名和密码生成token，token存入数据库，并写入cookie中，登录成功返回邮箱地址，重定向到欢迎⻚
     */
    @PostMapping("/login/{email}/{password}")
    public String login(@PathVariable @NotBlank(message = "邮箱不能为空") String email,
                        @PathVariable @NotBlank(message = "密码不能为空") String password) {
        return userService.login(email, password);
    }

    /**
     * 根据token查询用户登录邮箱接⼝
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable @NotBlank(message = "令牌不能为空") String token) {
        return userService.info(token);
    }

    /**
     * 根据token查询用户登录邮箱接⼝
     */
    @GetMapping("/filter/info/{token}")
    public String filterInfo(@PathVariable @NotBlank(message = "令牌不能为空") String token) {
        return userService.info(token);
    }
}
